﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Globalization

Partial Class Apps_Shares_Uc_staff_fv
    Inherits System.Web.UI.UserControl

    Public txtStaffNoClientID As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then
                InitCtrl()
            End If
            txtStaffNoClientID = FV.FindControl("txtStaffNo").ClientID.ToString()
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Sub InitCtrl()
        Dim BreadCrumpBG As HtmlControl = Page.Master.FindControl("tdBreadCrump")
        Dim BreadCrump As Literal = Page.Master.FindControl("ltrBreadCrump")
        'Change mode by querystring
        If Not String.IsNullOrEmpty(Request.QueryString("ReadOnly")) Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 13)
            fv.ChangeMode(FormViewMode.ReadOnly)
        ElseIf Not String.IsNullOrEmpty(Request.QueryString("Insert")) Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 14)
            fv.ChangeMode(FormViewMode.Insert)
        ElseIf Not String.IsNullOrEmpty(Request.QueryString("Edit")) Or String.IsNullOrEmpty(Request.QueryString("Update")) Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 15)
            fv.ChangeMode(FormViewMode.Edit)
        End If
    End Sub

    Protected Sub ddlDepartment_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            ' Comment by Boonyarit on March 9, 2011.
            ' Reason : Error on change department.
            'Dim ddlDepartment As DropDownList = fv.FindControl("ddlDepartment")
            'Dim ddlUserGroup As DropDownList = FV.FindControl("ddlUserGroup")

            'ddlUserGroup.DataTextField = "group_user_name"
            'ddlUserGroup.DataValueField = "group_user_code"
            'ddlUserGroup.DataSource = CodeMatrix.Utilities.CodeHelper.GetDataTable( _
            '"select group_user_code = 0 , group_user_name = 'กลุ่มผู้ใช้งาน' union all select group_user_code , group_user_name from group_user where department_code = @department_code and group_user_status = 1", _
            'New SqlParameter() {New SqlParameter("@department_code", ddlDepartment.SelectedValue)})
            'ddlUserGroup.DataBind()
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fv.DataBound
        Try
            If fv.CurrentMode() = FormViewMode.Edit Then
                Dim ddlUserGroup As DropDownList = fv.FindControl("ddlUserGroup")
                ddlUserGroup.SelectedValue = fv.DataItem("group_user_code")
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub fv_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs) Handles fv.ItemInserted
        Try
            e.ExceptionHandled = True
            If e.Exception Is Nothing Then
                ' log insert
                GenericHelper.GenerateScript(GenerateScriptLogType.Insert, LogMenuType.staff, e.Values)
                GenericHelper.ShowAlertSuccessful(Me)
            Else
                Throw New Exception(e.Exception.Message)
            End If
        Catch ex As Exception
            e.KeepInInsertMode = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub fv_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles fv.ItemInserting
        Try
            ' Server validation ---------------------------
            If e.Values("staff_no").ToString().Trim() = "" Then
                Throw New Exception("กรุณากรอกเลขประจำตัวพนักงาน")
            End If

            If e.Values("staff_firstname").ToString().Trim() = "" Then
                Throw New Exception("กรุณากรอกชื่อพนักงาน")
            End If

            If e.Values("staff_lastname").ToString().Trim() = "" Then
                Throw New Exception("กรุณากรอกนามสกุลพนักงาน")
            End If
            ' Server validation ---------------------------
            Dim cbChangeNewPassword As CheckBox = fv.FindControl("cbChangeNewPassword")
            e.Values("staff_status_login") = cbChangeNewPassword.Checked

            Dim ddlDepartment As DropDownList = fv.FindControl("ddlDepartment")
            e.Values("department_code") = ddlDepartment.SelectedValue

            Dim ddlUserGroup As DropDownList = fv.FindControl("ddlUserGroup")
            e.Values("group_user_code") = ddlUserGroup.SelectedValue

            Dim saltValue As String = System.Guid.NewGuid().ToString().Replace("-", "").Substring(0, 16)
            e.Values("staff_password") = CodeMatrix.Business.GenericData.GetNewPassword(saltValue)
            e.Values("staff_password_salt") = saltValue

        Catch ex As Exception
            e.Cancel = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub fv_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs) Handles fv.ItemUpdated
        Try
            e.ExceptionHandled = True
            If e.Exception Is Nothing Then
                ' log update
                GenericHelper.GenerateScript(GenerateScriptLogType.Update, LogMenuType.staff, e.NewValues, e.OldValues)
                'Redirect to list page
                Response.Redirect("staff_list.aspx?Edit=true")
            Else
                Throw New Exception(e.Exception.Message)
            End If
        Catch ex As Exception
            e.KeepInEditMode = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub fv_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles fv.ItemUpdating
        Try
            ' Server validation ---------------------------
            If e.NewValues("staff_no").ToString().Trim() = "" Then
                Throw New Exception("กรุณากรอกเลขประจำตัวพนักงาน")
            End If

            If e.NewValues("staff_firstname").ToString().Trim() = "" Then
                Throw New Exception("กรุณากรอกชื่อพนักงาน")
            End If

            If e.NewValues("staff_lastname").ToString().Trim() = "" Then
                Throw New Exception("กรุณากรอกนามสกุลพนักงาน")
            End If
            ' Server validation ---------------------------
            Dim ddlUserGroup As DropDownList = fv.FindControl("ddlUserGroup")
            e.NewValues("group_user_code") = ddlUserGroup.SelectedValue
        Catch ex As Exception
            e.Cancel = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub
End Class
